package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Goods;
import model.Order;

import decoder.ChineseDecoder;
import factory.DaoFactory;

/**
 * Servlet implementation class ModifyOrderServlet
 */
@WebServlet("/ModifyOrderServlet")
public class ModifyOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		int id = Integer.parseInt(request.getParameter("id"));
		Order order = DaoFactory.getOrderDao().findById(id);
		request.setAttribute("order", order);
		String address ="/jsp/ModifyOrder.jsp?name=" + name;
		RequestDispatcher dispatcher =request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		int id = Integer.parseInt(request.getParameter("id"));
		int quantity = 0;
		
		try {
			quantity = Integer.parseInt(request.getParameter("num"));
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfOrderQuantityError&name=" + name + "&id=" + id);
			return;
		}
		
		if (quantity <= 0) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfOrderQuantityError&name=" + name + "&id=" + id);
			return;
		}
		
		Order order = DaoFactory.getOrderDao().findById(id);
		Goods goods = DaoFactory.getGoodsDao().find(order.getGoodsId(), order.getDate());

		if (quantity > goods.getQuantity()) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfOrderTooMuchError&name=" + name + "&id=" + id);
			return;
		}
		
		order.setQuantity(quantity);
		order.setPrice(goods.getPrice() * quantity);
		
		boolean success = DaoFactory.getOrderDao().updateById(order);
		
		if (success) {
			response.sendRedirect(request.getContextPath() + "/OrderListServlet?name=" + name);
		} else {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfOrderDatabaseError&name=" + name + "&id=" + id);
		}
	}

}
